import {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean,
    GraphQLInt

} from 'graphql';

import Jurisdiction from '../../db/models/Jurisdiction'
import Role_jurisdiction_temp from '../../db/models/role_jurisdiction_temp'
import {
    getUserJurisdictionQuery
} from "../../db/query";


export let jurisdictionType = new GraphQLObjectType({
    name: 'jurisdiction',
    fields: {
        id: {
            type: GraphQLInt
        },
        Jurisdiction_name: {
            type: GraphQLString
        },
        Jurisdiction_path: {
            type: GraphQLString
        },
        Jurisdiction_type: {
            type: GraphQLString
        },
        parent_id: {
            type: GraphQLInt
        },
        type: {
            type: GraphQLInt
        },
        order: {
            type: GraphQLInt
        }
    }
})

export const getUserJurisdiction = {
    type: new GraphQLList(jurisdictionType),
    args: {
        id: {
            type: GraphQLInt
        },
        type: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return getUserJurisdictionQuery(params)
    }
}


export const jurisdictionList = {
    type: new GraphQLList(jurisdictionType),
    args: {},
    resolve(root, params, options) {
        return Jurisdiction.findAll({
            order: ["type"]

        })
    }
}


export const jurisdictionListByType = {
    type: new GraphQLList(jurisdictionType),
    args: {
        type: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Jurisdiction.findAll({
            where: {
                type: params.type
            }
        })
    }
}

export const jurisdictionOne = {
    type: jurisdictionType,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Jurisdiction.findOne({
            where: {
                id: params.id
            }
        })
    }
}
export const jurisdictionUpdate = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        Jurisdiction_name: {
            type: GraphQLString
        },
        Jurisdiction_path: {
            type: GraphQLString
        },
        Jurisdiction_type: {
            type: GraphQLString
        },
        parent_id: {
            type: GraphQLInt
        },
        type: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Jurisdiction.update({
            Jurisdiction_name: params.Jurisdiction_name,
            Jurisdiction_path: params.Jurisdiction_path,
            Jurisdiction_type: params.Jurisdiction_type,
            parent_id: params.parent_id,
            type: params.type
        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const jurisdictionAdd = {
    type: jurisdictionType,
    args: {
        Jurisdiction_name: {
            type: GraphQLString
        },
        Jurisdiction_path: {
            type: GraphQLString
        },
        Jurisdiction_type: {
            type: GraphQLString
        },
        parent_id: {
            type: GraphQLInt
        },
        type: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Jurisdiction.create({
            Jurisdiction_name: params.Jurisdiction_name,
            Jurisdiction_path: params.Jurisdiction_path,
            Jurisdiction_type: params.Jurisdiction_type,
            parent_id: params.parent_id,
            type: params.type
        })
    }
}

export const jurisdictionDel = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        Role_jurisdiction_temp.destroy({
            where: {
                jurisdiction_id: params.id
            }
        })
        return Jurisdiction.destroy({
            where: {
                id: params.id
            }
        })
    }
}
